package kr.etcsoft.examples.binding.viewer;

import java.util.ArrayList;
import java.util.List;

import kr.etcsoft.examples.binding.model.Catalog;
import kr.etcsoft.examples.binding.model.CatalogItem;

import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.property.list.IListProperty;
import org.eclipse.core.databinding.property.list.MultiListProperty;
import org.eclipse.jface.databinding.viewers.ObservableListTreeContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.ViewPart;

/**
 * http://git.eclipse.org/c/e4/org.eclipse.e4.databinding.git/tree/examples/org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/
 *
 * @author korman21@gmail.com
 */
public class SampleTreeView extends ViewPart {
    public static final String ID = "kr.etcsoft.examples.binding.viewer.sampletree";

    private TreeViewer viewer;

    @Override
    public void createPartControl(Composite parent) {
        parent.setLayout(new FillLayout());

        viewer = new TreeViewer(parent, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);

        Catalog catalog = makeData();

        IListProperty childrenProperty = new MultiListProperty(new IListProperty[] {
                BeanProperties.list("catalogs"),
                BeanProperties.list("items")
        });

        ObservableListTreeContentProvider contentProvider = new ObservableListTreeContentProvider(
                childrenProperty.listFactory(), null);
        viewer.setContentProvider(contentProvider);

        ObservableMapLabelProvider labelProvider = new ObservableMapLabelProvider(
                BeanProperties.value("name").observeDetail(contentProvider.getKnownElements())) {
            Image catalogImage = createCatalogImage();
            Image catalogItemImage = createCatalogItemImage();

            @Override
            public Image getImage(Object element) {
                if (element instanceof Catalog)
                    return catalogImage;
                if (element instanceof CatalogItem)
                    return catalogItemImage;
                return super.getImage(element);
            }

            @Override
            public void dispose() {
                catalogImage.dispose();
                catalogItemImage.dispose();
                super.dispose();
            }

            private Image createCatalogImage() {
                Display display = Display.getCurrent();
                Image catalogImage = new Image(display, 12, 12);
                GC gc = new GC(catalogImage);
                gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
                gc.fillArc(1, 1, 10, 10, 0, 360);
                gc.dispose();
                return catalogImage;
            }

            private Image createCatalogItemImage() {
                Display display = Display.getCurrent();
                Image catalogImage = new Image(display, 12, 12);
                GC gc = new GC(catalogImage);
                gc.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
                gc.fillPolygon(new int[] { 1, 10, 5, 1, 6, 1, 10, 10, 1, 10 });
                gc.dispose();
                return catalogImage;
            }
        };
        viewer.setLabelProvider(labelProvider);

        viewer.setInput(catalog);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private Catalog makeData() {
        List items;

        items = new ArrayList();
        items.add(new CatalogItem("Apple"));
        items.add(new CatalogItem("Orange"));

        Catalog fruits = new Catalog("Fruits");
        fruits.setCatalogs(items);

        items = new ArrayList();
        items.add(new CatalogItem("Peas"));
        items.add(new CatalogItem("Carrots"));
        items.add(new CatalogItem("Potatoes"));

        Catalog vegetables = new Catalog("Vegetables");
        vegetables.setItems(items);

        items = new ArrayList();
        items.add(fruits);
        items.add(vegetables);

        Catalog foods = new Catalog("Foods");
        foods.setCatalogs(items);

        items = new ArrayList();
        items.add(new CatalogItem("Own Hand"));
        foods.setItems(items);

        items = new ArrayList();
        items.add(foods);

        Catalog catalog = new Catalog("Main Catalog");
        catalog.setCatalogs(items);

        return catalog;
    }

    @Override
    public void setFocus() {
    }
}